﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net.Nntp.Cache
{
    using System.Collections.Generic;
    using System.Data.SqlServerCe;

    /// <summary>Provides the means to select articles from the database.</summary>
    internal static class InboundArticleSelector
    {
        internal static IEnumerable<IInboundArticle> GetArticles(Group group, long? referencesNumber)
        {
            using (var articleCommand = CommandHelper.Create(
                group.Connection,
                InboundArticleNames.Table,
                InboundArticleNames.GroupIdReferencesNumberIndex,
                DbRangeOptions.Match,
                new object[] { group.Id, referencesNumber },
                null))
            using (var articleResultSet = articleCommand.ExecuteResultSet(ResultSetOptions.Scrollable))
            {
                if (!articleResultSet.ReadLast())
                {
                    yield break;
                }

                do
                {
                    yield return new InboundArticle(group, articleResultSet);
                }
                while (articleResultSet.ReadPrevious());
            }
        }
    }
}
